import csv
import json
import os


hts=[]
with open('D:\\Work\\kusuri\\htwithusernums.csv','w',newline='\n',encoding='utf-8')as fin:
    writer = csv.writer(fin)
    writer.writerow(['Hashtag','Unique users','User with follower>10000','User with video>50','User with video>100'])
    for fname in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        if not fname.endswith('.json'):
            continue
        if not (fname.lower().startswith('hashtag')):
            continue
        ht=fname.split('_')[1]
        if ht in hts:
            continue
        hts.append(ht)
        uusers=[]
        u1w=0
        uv50=0
        uv100=0
        with open('D:\\Work\\Tool\\tiktok\\TikToks\\'+fname, 'r', encoding='utf-8', newline='\n') as filename_input:
            i=0
            for line in filename_input:
                z = json.loads(line)
                i += 1
                if i > 1500:
                    break
                if 'authorInfos' in z.keys():
                    if z['authorInfos']['uniqueId'] in uusers:
                        continue
                    uusers.append(z['authorInfos']['uniqueId'])
                    if int(z['authorStats']['followerCount'])>10000:
                        u1w+=1
                        if int(z['authorStats']['videoCount'])>50:
                            uv50+=1
                            if int(z['authorStats']['videoCount']) > 100:
                                uv100 += 1
                elif 'author' in z.keys():
                    if z['author']['uniqueId'] in uusers:
                        continue
                    uusers.append(z['author']['uniqueId'])
                    if int(z['authorStats']['followerCount'])>10000:
                        u1w+=1
                        if int(z['authorStats']['videoCount'])>50:
                            uv50+=1
                            if int(z['authorStats']['videoCount']) > 100:
                                uv100 += 1
        writer.writerow([ht,len(uusers),u1w,uv50,uv100])
